package com.xinchen.ssh.demo.core.dao;

import org.springframework.jdbc.core.SqlInOutParameter;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description：
 *
 * @author Xin Chen
 * @version 1.0
 * @date 2016/12/15 12:38
 * @see 2016/12/15 12:38 Created
 ****************************************/
public class StoredProcedures extends org.springframework.jdbc.object.StoredProcedure {

    /**
     * 输入参数Map
     */
    private transient Map<String, Object> inParameters = new HashMap<String, Object>();


    /**
     * @param storedProcedureName StoredProcedureName
     * @param dataSource dataSource
     */
    public StoredProcedures(String storedProcedureName, DataSource dataSource) {
        super.setDataSource(dataSource);
        super.setSql(storedProcedureName);
    }

    /**
     * 添加存储过程的输入参数
     *
     * @param name name
     * @param value value
     * @param type type
     * Date: 2011-5-26上午10:05:55
     */
    public void addInParameter(String name, Object value, int type) {
        inParameters.put(name, value);
        super.declareParameter(new SqlParameter(name, type));
        compile();

    }

    /**
     * 增加输出参数
     *
     * @param name name
     * @param type type
     * Date: 2011-5-26上午10:05:34
     */
    public void addOutParameter(String name, int type) {
        super.declareParameter(new SqlOutParameter(name, type));
        compile();
    }

    /**
     * 添加存储过程的输入，输出参数
     *
     * @param name name
     * @param value value
     * @param type type
     * Date: 2011-5-26上午10:05:55
     */
    public void addInOutParameter(String name, Object value, int type) {
        super.declareParameter(new SqlInOutParameter(name, type));
        compile();
    }

    /**
     * 执行存储过程
     * @return  Map Map
     * Date: 2011-5-26上午10:06:05
     */
    public Map<String, Object> execute() {
        return super.execute(inParameters);
    }

}
